/*
 *  Copyright (c) 2023-2025 苏州棱镜七彩信息科技有限公司 (Suzhou PrismAurora Information Technology Co.,Ltd)
 *  sbom-generate is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *           http://license.coscl.org.cn/MulanPSL2
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 * */
package com.ljqc.impl.file;

import com.ljqc.abs.Parser;
import com.ljqc.entity.FileInfo;
import com.ljqc.utils.FileUtils;

import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @ClassName FileParser
 * @Description
 * @Date 2025/3/18 11:02
 * @Version V1.0
 **/
public class FileParser implements Parser<FileInfo> {

    private static final Logger LOGGER = Logger.getLogger(FileParser.class.getName());

    @Override
    public List<FileInfo> parser(List<File> file) {
        LOGGER.log(Level.INFO, "开始解析文件信息，文件数量：{0}", file.size());
        List<FileInfo> result = file.stream().map(f -> {
            LOGGER.log(Level.FINE, "处理文件：{0}", f.getPath());
            FileInfo fileInfo = FileInfo.builder()
                    .name(f.getPath())
                    .size(f.length())
                    .sha256(FileUtils.calculateSHA256(f))
                    .build();
            LOGGER.log(Level.FINE, "文件信息解析完成：{0}，大小：{1}字节，SHA256：{2}", 
                    new Object[]{fileInfo.getName(), fileInfo.getSize(), fileInfo.getSha256()});
            return fileInfo;
        }).collect(Collectors.toList());
        LOGGER.log(Level.INFO, "文件信息解析完成，共处理 {0} 个文件", result.size());
        return result;
    }

}
